package com.color.admin.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.anji.captcha.controller.CaptchaController;
import com.color.admin.auth.entity.Captcha;
import com.color.admin.auth.entity.CaptchaCheck;
import com.color.admin.auth.service.CaptchaService;
import com.color.admin.captcha.controller.LoginController;
import com.color.admin.common.core.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HaiQiu
 * @date 2021/12/21
 * 验证码逻辑处理
 */
@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    private LoginController loginController;

    @Autowired
    private CaptchaController captchaController;

    @Value("${server.port}")
    private String port;


    @Override
    public Object captcha(Captcha captcha) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object> response = restTemplate.postForEntity("http://localhost:"+port+"/captcha/get",
                captcha, Object.class);
        return response.getBody();
    }



    @Override
    public Object check(CaptchaCheck check) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object> response = restTemplate.postForEntity("http://localhost:"+port+"/captcha/check",
                check, Object.class);
        return response.getBody();
    }

    @Override
    public void checkCaptcha(String captchaVerification) {
        RestTemplate restTemplate = new RestTemplate();
// 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("captchaVerification",captchaVerification);
        ResponseEntity<Object> response = restTemplate.postForEntity("http://localhost:"+port+"/captcha/login/check",
                paramMap, Object.class);
        Object checkCaptcha = response.getBody();
        String s = JSONObject.toJSONString(checkCaptcha);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String repCode =(String) jsonObject.get("repCode");
        String repMsg =(String) jsonObject.get("repMsg");
        //验证码校验失败，返回信息告诉前端
        //repCode  0000  无异常，代表成功
        //repCode  9999  服务器内部异常
        //repCode  0011  参数不能为空
        //repCode  6110  验证码已失效，请重新获取
        //repCode  6111  验证失败
        //repCode  6112  获取验证码失败,请联系管理员
        //repCode  6113  底图未初始化成功，请检查路径
        //repCode  6201  get接口请求次数超限，请稍后再试!
        //repCode  6206  无效请求，请重新获取验证码
        //repCode  6202  接口验证失败数过多，请稍后再试
        //repCode  6204  check接口请求次数超限，请稍后再试!
        String successCode = "0000";
        if (!successCode.equals(repCode)){
            throw new BaseException(repMsg);
        }
    }


}
